﻿package com.projects.xenia;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

import com.projects.xenia.MainCustomersListActivity.EfficientAdapter.ViewHolder;
import com.projects.xenia.MainGoodsCatalogActivity.EfficientAdapter;
import com.projects.xenia.global_modules.MainGlobalModule;

import android.app.ListActivity;

import android.content.Context;
import android.content.Intent;
import android.content.res.ColorStateList;
import android.database.Cursor;import android.graphics.Color;
import android.os.Bundle;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.view.WindowManager;
import android.view.View.OnClickListener;
import android.widget.BaseAdapter;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

public class MainDebtsListActivity extends ListActivity  {	
	
	private EfficientAdapter adap;
	
	private String id;
	private String customerdescr;	
	private String outletdescr;
	private String outletadress;
	private Double debtssumm;
	private Double debtsoversumm;
	
	private ImageView BackWardButton;
	
	private Cursor DebtsCursor;
	
	public void GetDebtsCursor () {
		
		try {
			DebtsCursor = MainGlobalModule.DBE.rawQuery
					("SELECT id, descr, outletdescr, outletadress, sum(restsumm), sum(restsummover)	FROM (SELECT c.id AS id, c.descr AS descr, c.outletdescr AS outletdescr, c.outletadress AS outletadress, sum( a.restsum) AS restsumm, sum(0) AS restsummover FROM xref_debts a INNER JOIN xref_customers c ON a.customers_id = c.id GROUP BY c.id, c.descr, c.outletdescr, c.outletadress UNION SELECT c.id AS id, c.descr AS descr, c.outletdescr AS outletdescr, c.outletadress AS outletadress, sum(0) AS restsumm, sum( a.restsum) AS restsummover FROM xref_debts a INNER JOIN xref_customers c ON a.customers_id = c.id WHERE date(docpaydate) <= date('now') GROUP BY c.id, c.descr, c.outletdescr, c.outletadress) GROUP BY id, descr, outletdescr, outletadress order by descr,outletdescr",null);
			} catch (Exception e) {
		}		
		
	}
	
	@Override
	public void onCreate(Bundle savedInstanceState) {
		
		super.onCreate(savedInstanceState);
		
        requestWindowFeature(Window.FEATURE_NO_TITLE);            
        getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,WindowManager.LayoutParams.FLAG_FULLSCREEN);
		setContentView(R.layout.debts_form);
		
		BackWardButton = (ImageView)findViewById(R.id.cust_backward);
		BackWardButton.setOnClickListener(new OnClickListener() {			
			@Override
			public void onClick(View v) {
				finish();
			}
		});
		
		GetDebtsCursor ();		
		
		adap = new EfficientAdapter(this);
		setListAdapter(adap);
		
		MainGlobalModule.setBackGround(getListView());
		
	}	

	
	@Override
	protected void onListItemClick(ListView l, View v, int position, long id) {
	   
	    	DebtsCursor.moveToPosition(position);
	    	MainGlobalModule.SelectedId = DebtsCursor.getString(0);
	    	Intent intent = new Intent();
	    	intent.setClass(this,MainDebtsDetailsActivity.class);
	    	startActivity (intent);
			
  	   
	  }
	
	public class EfficientAdapter extends BaseAdapter  {
		
		private LayoutInflater mInflater;
		public Context context;
		 
		public EfficientAdapter(Context context) {
		// Cache the LayoutInflate to avoid asking for a new one each time.
		mInflater = LayoutInflater.from(context);
		this.context = context;
		}
		 
		public View getView(final int position, View convertView, ViewGroup parent) {

		ViewHolder holder;
		
		if (convertView == null) {
			
		convertView = mInflater.inflate(R.layout.debts_form_list, null);
		
		holder = new ViewHolder();		
		
		holder.DebtsCustomer = (TextView) convertView.findViewById(R.id.DebtsCustomer); 
		holder.DebtsOutlet = (TextView) convertView.findViewById(R.id.DebtsOutlet);		
		holder.DebtsSumm = (TextView) convertView.findViewById(R.id.DebtsSumm);
		holder.DebtsOverSumm = (TextView) convertView.findViewById(R.id.DebtsOverSumm);		
		
		
		convertView.setTag(holder);
		
		}
		
		else
			
		{
		holder = (ViewHolder) convertView.getTag();
		}		 
		
		
		getItem(position);
				
		holder.DebtsCustomer.setText(customerdescr); 
		holder.DebtsOutlet.setText(outletdescr+outletadress);
		
		holder.DebtsSumm.setText(String.format("%.2f", debtssumm));
		
		holder.DebtsOverSumm.setText(String.format("%.2f", debtsoversumm));
		
		if (debtsoversumm>0) {
			holder.DebtsOverSumm.setTextColor(Color.RED);			
		}
		else{
			holder.DebtsOverSumm.setTextColor(Color.GRAY);
		}
			
		
		
		
		
		return convertView;
		
		}
		 
		class ViewHolder {
		
		TextView DebtsCustomer;		
		TextView DebtsOutlet;
		TextView DebtsSumm;		
		TextView DebtsOverSumm;		
		
		}
		 
				 
		@Override
		public long getItemId(int position) {

		return 0;
		}
		 
		@Override
		public int getCount() {

		return DebtsCursor.getCount();
		}
		 
		@Override
		public Object getItem(int position) {
			
			DebtsCursor.moveToPosition(position);			
			id = DebtsCursor.getString(0);
			customerdescr = DebtsCursor.getString(1);			
			outletdescr = DebtsCursor.getString(2);
			outletadress = DebtsCursor.getString(3);			
			debtssumm = DebtsCursor.getDouble(4);
			debtsoversumm = DebtsCursor.getDouble(5);
			
		return id;
		
		}
		
		private class OnItemClickListener implements OnClickListener {
			
		    private int mPosition;
		    OnItemClickListener(int position){
		        mPosition = position;		        
		    }
		    
		    @Override
		    public void onClick(View v) {	        			        	  
	        	  
		    }       
		    
		}	
}
	
	
}